package com.butterfly.mvc.servlet;

import java.io.File;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by DouDou on 2016/9/28.
 */
public class PackageScan {
    public PackageScan() {
    }

    /**
     * 扫描指定路径，解析类
     * @param path
     * @return
     */
    public static Map<String,ActionBean> scan(String path,Map<String,ActionBean> serviceMap){
        String packagePath = path.replace(".",File.separator);
        URL url = Thread.currentThread().getContextClassLoader().getResource(packagePath);
        File  file = new File(url.getFile());
        if(!file.exists()){
            throw new RuntimeException("路径有误");
        }

        if(file.isDirectory()) {
            File[] files = file.listFiles();
            for (int i = 0; i < files.length; i++) {
                File f = files[i];
                if (f.isDirectory()) {
                    scan(path+"."+f.getName(),serviceMap);
                }
                if(f.isFile()) {
                    try {
                        ClassScan.scan(path+"."+f.getName(), serviceMap);
                    } catch (ClassNotFoundException e) {
                        e.printStackTrace();
                    }
                }
            }
        }

        if(file.isFile()){
            try {
                ClassScan.scan(path+"."+file.getName(), serviceMap);
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }
        }

        return serviceMap;
    }

    public static void main(String[] args){
        String str = "com.butterfly.mvc.action";
        String s = str.replace(".","/");
    }
}
